import { createServer } from "node:http";
import dotenv from "dotenv";

import { getAllUsers, createUserHandler, getUserHandler } from "./users.js";

dotenv.config();
const PORT = process.env.PORT || 8002;

const server = createServer((req, res) => {
  if (req.url === "/api/users/all" && req.method === "GET") {
    getAllUsers(req, res);
  } else if (req.url.match("/api/users/([0-9]+)") && req.method === "GET") {
    getUserHandler(req, res);
  } else if (req.url === "/api/users/add" && req.method === "POST") {
    createUserHandler(req, res);
  } else {
    res.writeHead(404, {
      "content-type": "application/json",
    });

    res.end(JSON.stringify({ message: "Rout not found" }));
  }
});

server.listen(PORT, () => {
  console.log(`Server run on ${PORT}`);
});
